<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>图片滤镜——GPUImage | 海晨忆的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="description" content="&amp;emsp;&amp;emsp;GPUImage图片滤镜处理的第三方开源库，对照IOS版的GPUImage写的,部分功能尚未完善，目前也有很多种滤镜，常用的滤镜基本上都有，请先浏览一遍github上面的用法。">
<meta property="og:type" content="article">
<meta property="og:title" content="图片滤镜——GPUImage">
<meta property="og:url" content="https://haichenyi.com/2019/06/11/图片滤镜——GPUImage/index.html">
<meta property="og:site_name" content="海晨忆的博客">
<meta property="og:description" content="&amp;emsp;&amp;emsp;GPUImage图片滤镜处理的第三方开源库，对照IOS版的GPUImage写的,部分功能尚未完善，目前也有很多种滤镜，常用的滤镜基本上都有，请先浏览一遍github上面的用法。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-06-11T14:38:34.991Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="图片滤镜——GPUImage">
<meta name="twitter:description" content="&amp;emsp;&amp;emsp;GPUImage图片滤镜处理的第三方开源库，对照IOS版的GPUImage写的,部分功能尚未完善，目前也有很多种滤镜，常用的滤镜基本上都有，请先浏览一遍github上面的用法。">
  
    <link rel="alternate" href="/atom.xml" title="海晨忆的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/uploads/artistic_image/head.jpg">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/css/highlight.css">
  <script>
  let antiquityStorage = window.sessionStorage.getItem('antiquitySessionStorage');
  if (antiquityStorage == '' || antiquityStorage == null) {
    var antiquityLoader = '<div id="loaderbox"><div class="loader"><div class="load-roll"><div class="load-top"></div><div class="load-right"></div><div class="load-bottom"></div></div></div></div>';
    document.write(antiquityLoader);
    document.body.style.overflow = 'hidden'
  }
  </script>
</head>

<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" style="background-image: url(/uploads/artistic_image/bg.jpg)" title="背景图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-right">
      
        <a class="nav-item" href="/" title
        
        >首页</a>
      
        <a class="nav-item" href="/archives" title
        
        >归档</a>
      
        <a class="nav-item" href="/about" title
        
        >简历</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-图片滤镜——GPUImage" class="article global-container article-type-post" itemscope itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      图片滤镜——GPUImage
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/2019/06/11/图片滤镜——GPUImage/" class="article-date">
  <time datetime="2019-06-11T14:32:53.000Z" itemprop="datePublished">2019-06-11</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Android-常用功能/">Android -常用功能</a>
  </div>

    
  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-cloud doorframe mac" itemprop="articleBody">
      
        <p>&emsp;&emsp;<a href="https://github.com/cats-oss/android-gpuimage" target="_blank" rel="noopener">GPUImage</a>图片滤镜处理的第三方开源库，对照IOS版的GPUImage写的,部分功能尚未完善，目前也有很多种滤镜，常用的滤镜基本上都有，请先浏览一遍github上面的用法。</p>
<a id="more"></a>
<h2 id="依赖的库"><a href="#依赖的库" class="headerlink" title="依赖的库"></a>依赖的库</h2><div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">repositories &#123;</span><br><span class="line">    jcenter()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">dependencies &#123;</span><br><span class="line">//这个版本号2.x.x，具体的数字看，github官方说明</span><br><span class="line">//README.md下方Download后面的版本号</span><br><span class="line">    implementation &apos;jp.co.cyberagent.android:gpuimage:2.x.x&apos;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<h2 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h2><h3 id="一、图片变形"><a href="#一、图片变形" class="headerlink" title="一、图片变形"></a>一、图片变形</h3><p>&emsp;&emsp;Android版目前没有IOS那么多类，那么多用法，我们用的最多的就是GPUImageView这个自定义view，继承的FrameLayout，并不是继承的ImageView，所以，它这里显示图片的时候，会有<strong>图片变形的问题</strong>。我的<strong><em>处理方法：</em></strong></p>
<ol>
<li>先用Glide获取图片的宽高</li>
<li>然后获取GPUImageView的LayoutParams，动态设置控件的宽高</li>
</ol>
<h3 id="二、内存溢出"><a href="#二、内存溢出" class="headerlink" title="二、内存溢出"></a>二、内存溢出</h3><ol>
<li>图片过大造成的内存溢出，压缩图片，推荐使用鲁班压缩</li>
<li>频繁使用GPUImage获取Bitmap的getBitmapWithFilterApplied()方法，造成Bitmap过多的内存泄漏，推荐用WeakReference(弱引用)标记Bitmap，GC自动回收</li>
<li>显示大图和缩略图，一般都是一个大图和多种添加滤镜后的效果图(这个是缩略图)，这里缩略图再通过getBitmapWithFilterApplied获取之前，最好吧原图按照规则缩小之后再获取显示，这样也能尽可能的减少内存的占用，点击缩略图显示大的效果图的时候，并不是改变bitmap，是给gpuIamgeView对象设置你点击目标图使用滤镜即可，这样也可以避免内存过多的消耗</li>
</ol>
<h3 id="三、滤镜添加"><a href="#三、滤镜添加" class="headerlink" title="三、滤镜添加"></a>三、滤镜添加</h3><ol>
<li>单一滤镜的添加</li>
</ol>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//这里以添加黑白滤镜为例</span><br><span class="line">GPUImageView gpuImageView = findViewById(R.id.img);</span><br><span class="line">gpuImageView.setImage(bitmap);</span><br><span class="line">gpuImageView.setFilter(new GPUImageGrayscaleFilter());</span><br></pre></td></tr></table></figure></div>
<ol>
<li>组合滤镜的添加GPUImageFilterGroup</li>
</ol>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GPUImageView gpuImageView = findViewById(R.id.img);</span><br><span class="line">gpuImageView.setImage(bitmap);</span><br><span class="line">GPUImageFilterGroup filterGroup = new GPUImageFilterGroup();</span><br><span class="line">//把你需要添加的滤镜放到GPUImageFilterGroup容器里面，</span><br><span class="line">//这里我添加了灰色滤镜，曝光度滤镜和饱和度滤镜理论上可以添加无数个</span><br><span class="line">filterGroup.add(new GPUImageGrayscaleFilter());</span><br><span class="line">filterGroup.add(new GPUImageExposureFilter());</span><br><span class="line">filterGroup.add(new GPUImageSaturationFilter());</span><br><span class="line">//把这个容器添加到GPUImageView</span><br><span class="line">gpuImageView.setFilter(filterGroup);</span><br></pre></td></tr></table></figure></div>
<ol>
<li>多张图片的滤镜</li>
</ol>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">//这里以GPUImageTwoInputFilter为例（可以加到组合滤镜里面）,它有多个子类</span><br><span class="line">//我们这里用GPUImageChromaKeyBlendFilter为例</span><br><span class="line">//实现的效果是一个过渡效果，从原图过渡到目标图</span><br><span class="line">GPUImageView gpuImageView = findViewById(R.id.img);</span><br><span class="line">//设置原图</span><br><span class="line">gpuImageView.setImage(bitmap);</span><br><span class="line">//新建滤镜对象，并且把目标图设置给滤镜</span><br><span class="line">GPUImageChromaKeyBlendFilter keyBlendFilter = new GPUImageChromaKeyBlendFilter();</span><br><span class="line">//设置目标图</span><br><span class="line">keyBlendFilter.setBitmap(bitmap1);</span><br><span class="line">//平滑的过渡方法，改变参数的值即可</span><br><span class="line">keyBlendFilter.setSmoothing(progressFloat);</span><br><span class="line">//把滤镜设置给GPUImageView</span><br><span class="line">gpuImageView.setFilter(keyBlendFilter);</span><br></pre></td></tr></table></figure></div>
<h3 id="四、微调（敏感度问题）"><a href="#四、微调（敏感度问题）" class="headerlink" title="四、微调（敏感度问题）"></a>四、微调（敏感度问题）</h3><p>&emsp;&emsp;只要构造方法，方法带参数的，都可以微调，这里微调的取值范围，Filter源码的类注释上面都有。如果，我们把seekBar的取值范围设置成类注释上面的范围，你滑动很小距离的seekBar，图片变化就会很大，所以，我们一般都是缩小范围再使用。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">//第一个参数seekBar是最大值，第二个参数是最小值，第三个参数是默认值，第四个参数是seekbar分几段</span><br><span class="line">//mapSeekBarBean.put(TYPE_SATURATION, new SeekBarBean(2, 0, 0.5f, 10));</span><br><span class="line">mapSeekBarBean.put(TYPE_SATURATION, new SeekBarBean(100, 0, 50f, 2));</span><br><span class="line">//mapSeekBarBean.put(TYPE_BRIGHTNESS, new SeekBarBean(1, -1, 0.5f, 10));</span><br><span class="line">mapSeekBarBean.put(TYPE_BRIGHTNESS, new SeekBarBean(100, 0, 50f, 2));</span><br><span class="line">//mapSeekBarBean.put(TYPE_EXPOSURE, new SeekBarBean(10, -10, 0.5f, 0));</span><br><span class="line">mapSeekBarBean.put(TYPE_EXPOSURE, new SeekBarBean(100, 0, 50f, 2));</span><br><span class="line">//mapSeekBarBean.put(TYPE_CONTRAST, new SeekBarBean(4, 0, 0.25f, 0));</span><br><span class="line">mapSeekBarBean.put(TYPE_CONTRAST, new SeekBarBean(100, 0, 25f, 2));</span><br><span class="line">mapSeekBarBean.put(TYPE_POSTERIZE, new SeekBarBean(256, 0, 100f, 3));</span><br><span class="line">//mapSeekBarBean.put(TYPE_HIGH_LIGHT_SHADOW, new SeekBarBean(1, 0, 0f, 0));</span><br><span class="line">mapSeekBarBean.put(TYPE_HIGH_LIGHT_SHADOW, new SeekBarBean(100, 0, 0f, 2));</span><br><span class="line">mapSeekBarBean.put(TYPE_SHARPEN, new SeekBarBean(100, 0, 50f, 3));</span><br><span class="line">//mapSeekBarBean.put(TYPE_GAMMA, new SeekBarBean(3, 0, 0.33f, 0));</span><br><span class="line">mapSeekBarBean.put(TYPE_GAMMA, new SeekBarBean(100, 0, 33f, 3));</span><br><span class="line">//mapSeekBarBean.put(TYPE_OPACITY, new SeekBarBean(1, 0, 1f, 0));</span><br><span class="line">mapSeekBarBean.put(TYPE_OPACITY, new SeekBarBean(100, 0, 100f, 2));</span><br><span class="line">//mapSeekBarBean.put(TYPE_VIBRANCE, new SeekBarBean(1, 0, 0f, 0));</span><br><span class="line">mapSeekBarBean.put(TYPE_VIBRANCE, new SeekBarBean(100, 0, 0f, 2));</span><br></pre></td></tr></table></figure></div>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">//这里是最终设置的值</span><br><span class="line">switch (entrySet.getKey()) &#123;</span><br><span class="line">                case TYPE_SATURATION:</span><br><span class="line">                    //最后面*2是范围（0,2）</span><br><span class="line">                    float f1 = (entrySet.getValue().getProgress() / entrySet.getValue().getMax()) * 2;</span><br><span class="line">                    filters.add(new GPUImageSaturationFilter(f1));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_BRIGHTNESS:</span><br><span class="line">                    float f2 = entrySet.getValue().getProgress();</span><br><span class="line">                    if (f2 == 50) &#123;</span><br><span class="line">                        f2 = 0f;</span><br><span class="line">                    &#125; else &#123;</span><br><span class="line">                        //后面的*0.7是范围（-1,1），以中间0为准，分成两部分（-1,0），（0,1）</span><br><span class="line">                        //负数为变暗，正数为变亮,本应该*1</span><br><span class="line">                        f2 = (float) (((f2 - 50) / 50) * 0.4);</span><br><span class="line">                    &#125;</span><br><span class="line">                    filters.add(new GPUImageBrightnessFilter(f2));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_EXPOSURE:</span><br><span class="line">                    float f3 = entrySet.getValue().getProgress();</span><br><span class="line">                    if (f3 == 50) &#123;</span><br><span class="line">                        f3 = 0f;</span><br><span class="line">                    &#125; else &#123;</span><br><span class="line">                        //后面的*1是范围（-10,10），以中间0为准，分成两部分（-10,0），（0,10）</span><br><span class="line">                        //负数为变暗，正数为变亮，本应该*10</span><br><span class="line">                        f3 = ((f3 - 50) / 50) * 1;</span><br><span class="line">                    &#125;</span><br><span class="line">                    filters.add(new GPUImageExposureFilter(f3));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_CONTRAST:</span><br><span class="line">                    //最后面*4是范围（0,4）</span><br><span class="line">                    float f4 = (entrySet.getValue().getProgress() / entrySet.getValue().getMax()) * 4;</span><br><span class="line">                    filters.add(new GPUImageContrastFilter(f4));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_POSTERIZE:</span><br><span class="line">                    filters.add(new GPUImagePosterizeFilter((int) entrySet.getValue().getProgress()));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_HIGH_LIGHT_SHADOW:</span><br><span class="line">                    GPUImageHighlightShadowFilter highlightShadowFilter = new GPUImageHighlightShadowFilter();</span><br><span class="line">                    float f9 = (entrySet.getValue().getProgress() / entrySet.getValue().getMax()) * 1;</span><br><span class="line">                    highlightShadowFilter.setHighlights(1 - f9);</span><br><span class="line">                    highlightShadowFilter.setShadows(f9);</span><br><span class="line">                    filters.add(highlightShadowFilter);</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_SHARPEN:</span><br><span class="line">                    float f5 = entrySet.getValue().getProgress();</span><br><span class="line">                    if (f5 == 50) &#123;</span><br><span class="line">                        f5 = 0f;</span><br><span class="line">                    &#125; else &#123;</span><br><span class="line">                        //后面的*4是范围（-4,4），以中间0为准，分成两部分（-4,0），（0,4）</span><br><span class="line">                        //负数为变暗，正数为变亮，本应该*4</span><br><span class="line">                        f5 = ((f5 - 50) / 50) * 4;</span><br><span class="line">                    &#125;</span><br><span class="line">                    filters.add(new GPUImageSharpenFilter(f5));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_GAMMA:</span><br><span class="line">                    //最后面*3是范围（0,3）</span><br><span class="line">                    float f6 = (entrySet.getValue().getProgress() / entrySet.getValue().getMax()) * 3;</span><br><span class="line">                    filters.add(new GPUImageGammaFilter(f6));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_OPACITY:</span><br><span class="line">                    float f7 = (entrySet.getValue().getProgress() / entrySet.getValue().getMax()) * 1;</span><br><span class="line">                    filters.add(new GPUImageOpacityFilter(f7));</span><br><span class="line">                    break;</span><br><span class="line">                case TYPE_VIBRANCE:</span><br><span class="line">                    float f8 = (entrySet.getValue().getProgress() / entrySet.getValue().getMax()) * 1;</span><br><span class="line">                    filters.add(new GPUImageVibranceFilter(f8));</span><br><span class="line">                    break;</span><br><span class="line">                default:</span><br><span class="line">            &#125;</span><br></pre></td></tr></table></figure></div>
<h2 id="用法"><a href="#用法" class="headerlink" title="用法"></a>用法</h2><p>&emsp;&emsp;上面的注意事项里面已经说了简单的用法了，怎么获取滤镜后的图片呢？</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//这个方法是获取bitmap对象，至于怎么保存，那就是你自己做了</span><br><span class="line">gpuImageView.getGPUImage().getBitmapWithFilterApplied();</span><br><span class="line"></span><br><span class="line">//当然，库也提供了保存图片的方法：保存的文件夹名称，文件名字，回调方法</span><br><span class="line">gpuImageView.saveToPictures(folderName,fileName,OnPictureSavedListener)</span><br><span class="line">//其中回调方法里面返回的uri，不能直接传给File，会找不到路径，需要转换一下</span><br></pre></td></tr></table></figure></div>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">     * 根据Uri获取文件的路径</span><br><span class="line">     *</span><br><span class="line">     * @param context    context</span><br><span class="line">     * @param contentURI uri</span><br><span class="line">     * @return 文件路径</span><br><span class="line">     */</span><br><span class="line">    public static String getRealPathFromURI(Context context, Uri contentURI) &#123;</span><br><span class="line">        String result;</span><br><span class="line">        Cursor cursor = context.getContentResolver().query(contentURI,</span><br><span class="line">                new String[]&#123;MediaStore.Images.ImageColumns.DATA&#125;,</span><br><span class="line">                null, null, null);</span><br><span class="line">        if (cursor == null) &#123;</span><br><span class="line">            result = contentURI.getPath();</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            cursor.moveToFirst();</span><br><span class="line">            int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);</span><br><span class="line">            result = cursor.getString(index);</span><br><span class="line">            cursor.close();</span><br><span class="line">        &#125;</span><br><span class="line">        return result;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></div>
<p>项目就不贴出来了。</p>

      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/2019/06/11/Android——Tensorflow-Lite简单使用/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            Android——Tensorflow-Lite简单使用
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/2018/10/11/JDBC的简单使用——MySQL/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">JDBC的简单使用——MySQL</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    
  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box avatar-item">
    <img class="avatar" src="/uploads/artistic_image/head.jpg" title="头像来自网络"></img>
    <h3 class="avatar-name">
      
        海晨忆
      
    </h3>
    <p class="avatar-slogan">
      先谋生，再谋爱。人间值得，未来可期。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android-Socket/">Android -Socket</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-加密算法/">Android -加密算法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-常用功能/">Android -常用功能</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-框架/">Android -框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-源码解析/">Android -源码解析</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-自定义view/">Android -自定义view</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-设计模式/">Android -设计模式</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA-并发/">JAVA -并发</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java-SpringBoot/">Java -SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/kotlin/">kotlin</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-JDBC/">数据库 -JDBC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-MySQL/">数据库 -MySQL</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/27/JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁/">JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁</a>
          </li>
        
          <li>
            <a href="/2021/07/19/JAVA—线程同步器AQS/">JAVA—线程同步器AQS</a>
          </li>
        
          <li>
            <a href="/2021/07/07/Android模拟点击/">Android模拟点击</a>
          </li>
        
          <li>
            <a href="/2019/11/27/SpringBoot系列-消息-RabbitMQ-（二十）/">SpringBoot系列-消息(RabbitMQ)（二十）</a>
          </li>
        
          <li>
            <a href="/2019/11/13/SpringBoot系列-Docker（十九）/">SpringBoot系列-Docker（十九）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
      <div class="widget-box">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      
        <a class="hrf" style="display: block;" href="https://github.com/haichenyi" title target='_blank'
        >Github</a>
      
        <a class="hrf" style="display: block;" href="https://www.jianshu.com/u/6077ee440c37" title target='_blank'
        >简书</a>
      
        <a class="hrf" style="display: block;" href="https://blog.csdn.net/qq_27634797" title target='_blank'
        >CSDN</a>
      
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box footers global-width">
    &copy;2017-2021 海晨忆 &nbsp;&nbsp;
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
if (!window.jQuery) {
var script = document.createElement('script');
script.src = "/js/jquery-2.0.3.min.js";
document.body.write(script);
}
</script>

  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/about" class="mobile-nav-link">简历</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>